package test.src.awt_swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PinBall {
    //创建窗口对象
    Frame frame = new Frame("弹珠游戏");

    //定义桌面属性
    private final int TABLE_WIDTH = 300;
    private final int TABLE_HEIGHT = 400;

    //定义球拍的大小
    private final int RACKET_WIDTH = 60;
    private final int RACKET_HEIGHT = 20;

    //小球的大小
    private final int BALL_SIZE = 15;

    //定义变量，记录小球的坐标
    private int BALL_X = 120;
    private int BALL_Y = 20;

    //定义变量，记录球拍的坐标
    private int RACKET_X = 120;
    private final int RACKET_Y = TABLE_HEIGHT - 40;

    //定义小球的移动速度
    private int SPEED_X = 2;
    private int SPEED_Y = 4;

    //标识游戏是否已经结束
    private boolean isOver = false;

    //声明一个定时器
    private Timer timer;


    //自定义类，继承canvas，充当画布

    private class MyCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
            //在这里绘制内容
            if (isOver) {
                //游戏结束
                g.setColor(Color.BLUE);
                g.setFont(new Font("Times", Font.BOLD, 30));
                g.drawString("游戏结束!", 50, 200);
            } else {
                //游戏中
                //绘制小球
                g.setColor(Color.red);
                g.fillOval(BALL_X, BALL_Y, BALL_SIZE, BALL_SIZE);

                //绘制球拍
                g.setColor(Color.pink);
                g.fillRect(RACKET_X, RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);
            }
        }
    }

    //创建绘画区域
    MyCanvas myCanvas = new MyCanvas();

    public void init() {
        //组装视图，游戏逻辑的控制


        //完成球拍坐标的变化
        KeyListener keyListener = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int KEY_CODE = e.getKeyCode();
                if (KEY_CODE == KeyEvent.VK_LEFT) {
                    if (RACKET_X - 10 >= 0) {
                        RACKET_X -= 10;
                    }
                }
                if (KEY_CODE == KeyEvent.VK_RIGHT) {
                    if (RACKET_X + 60 < TABLE_WIDTH) {
                        RACKET_X += 10;
                    }
                }
            }
        };

        //给Frame和canvas注册监听器
        frame.addKeyListener(keyListener);
        myCanvas.addKeyListener(keyListener);

        ActionListener task = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //根据边界范围，修正速度;
                if (BALL_X <= 0 || BALL_X >= (TABLE_WIDTH - BALL_SIZE)) {
                    SPEED_X = -SPEED_X;
                }
                if (BALL_Y <= 0 || (BALL_Y > RACKET_Y-BALL_SIZE && BALL_X > RACKET_X && BALL_X < RACKET_X + RACKET_WIDTH)) {
                    SPEED_Y = -SPEED_Y;
                }
                if (BALL_Y > TABLE_HEIGHT) {
                    timer.stop();
                    isOver = true;
                    myCanvas.repaint();
                }
                //更新小球的坐标，重新绘制界面
                BALL_X += SPEED_X;
                BALL_Y += SPEED_Y;

                //重绘界面
                myCanvas.repaint();
            }
        };

        //小球坐标的控制
        timer = new Timer(40, task);
        timer.start();

        myCanvas.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));
        frame.add(myCanvas);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new PinBall().init();
    }
}
